package algorithm.leetcode.medium;

/*
 给定一个二叉树，在树的最后一行找到最左边的值。
 */

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2021/1/19
 */
public class Question513 {

    public static void main(String[] args) {

    }

    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode node;
        int val = 0;
        while (!queue.isEmpty()) {
            node = queue.poll();
            val = node.val;
            if (node.right != null) {
                queue.offer(node.right);
            }
            if (node.left != null) {
                queue.offer(node.left);
            }
        }
        return val;
    }


    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
